package code13_collection.list;

import java.util.Scanner;

public class HomeWork {

	public static void main(String[] args) {
		HomeWork hw = new HomeWork();
		hw.start();
	}
	
	public void start() {
		while(true){
			// 打印棋盘
			printTable();
			// 没有地方可以落子时, 退出循环, 则会自动结束程序
			if(step >= MAX_STEP){
				System.out.println("游戏结束");
				break;
			}
			// 落子操作
			dropChess();
		}
	}
	
	private String[][] chesses;
	private Scanner sc = new Scanner(System.in);
	/**
	 * 步数
	 */
	private int step;
	private static int MAX_STEP;
	/**
	 * 棋盘尺寸
	 */
	private int size = 9;
	private static String temp = " ";
	
	public HomeWork() {
		// 最大落子个数
		MAX_STEP = size * size;
		chesses = new String[size][size];
		for (int i = 0; i < size; i++) {
			for (int j = 0; j < size; j++) {
				chesses[i][j] = temp;
			}
		}
	}
	
	private void printTable() {
		for (int i = 0; i < size; i++) {
			System.out.print("   ");
			System.out.print(i);
		}
		System.out.println();
		
		System.out.print(" +");
		for (int i = 0; i < size; i++) {
			System.out.print("---+");
		}
		System.out.println();
		
		for (int i = 0; i < chesses.length; i++) {
			System.out.print(i);
			System.out.print("|");
			for (int j = 0; j < chesses[i].length; j++) {
				System.out.print(" ");
				System.out.print(chesses[i][j]);
				System.out.print(" |");
			}
			
			System.out.print("\n +");
			for (int m = 0; m < size; m++) {
				System.out.print("---+");
			}
			System.out.println();
		}
	}
	
	private void dropChess() {
		String player = "○";
		if(step%2 == 0){
			player = "●";
		}
		System.out.println("请 [" + player + "方] 输入落子位置(x,y):");
		String next = sc.next();
		String[] arr = next.split(",");
		int x = Integer.parseInt(arr[0]);
		int y = Integer.parseInt(arr[1]);
		
		//1.判断坐标是否合法
		if(x < 0 || y < 0 || x >= size || y >= size){
			System.out.println("输入的坐标超出范围,请重新输入!");
			dropChess();
			return;
		}
		
		//2.判断当前坐标处是否有棋子
		String mark = chesses[y][x];
		if(!mark.equals(" ")){
			System.out.println("该位置已经有棋子,请重新输入!");
			dropChess();
			return;
		}
		
		System.out.println("您落子的位置是:" + x + "," + y);
		
		//3.交替落子
		chesses[y][x] = player;
		// 步数统计
		step++;
		
		// 判断输赢
		if(isWin(y, x, player)){
			System.out.println("["+ player +"] is winner!");
			// 退出程序
//			System.exit(0);
			step = MAX_STEP;
		}
	}

	/**
	 * 判断当前 player 是否赢了
	 * @param y
	 * @param x
	 * @param player
	 * @return true赢了
	 */
	private boolean isWin(int y, int x, String player) {
		return count1(y, x, player) >= 5 
				|| count2(y, x, player) >= 5 
				|| count3(y, x, player) >= 5 
				|| count4(y, x, player) >= 5;
	}
	
	// 水平方向
	private int count1(int y, int x, String player) {
		int count = 0;
		// 向右
		int x1 = x;
		do {
			count++;
			// 向右移
			x1++;
			// 判断是否出界
			if(x1 >= size){
				break;
			}
		} while (player.equals(chesses[y][x1]));
		
		count--;// 纠正使用do while的 问题
		
		// 向左
		int x2 = x;
		do {
			count++;
			// 向左移
			x2--;
			// 判断是否出界
			if(x2 < 0){
				break;
			}
		} while (player.equals(chesses[y][x2]));
		
		return count;
	}
	// 垂直方向
	private int count2(int y, int x, String player) {
		int count = 1;
		// 向上
		int y1 = y - 1;
		// 下一个位置是合法的
		if(y1 >= 0){
			while(player.equals(chesses[y1][x])){
				count++;
				y1--;
				if(y1 < 0){
					// 超出界限
					break;
				}
			}
		}
		
		// 向下
		int y2 = y + 1;
		// 下一个位置是合法的
		if(y2 < size){
			while(player.equals(chesses[y2][x])){
				count++;
				y2++;
				if(y2 >= size){
					// 超出界限
					break;
				}
			}
		}
		return count;
	}
	// 右斜/
	private int count3(int y, int x, String player) {
		int count = 1;
		// TODO: 落子个数计算1
		return count;
	}
	// 左斜\
	private int count4(int y, int x, String player) {
		int count = 1;
		// TODO: 落子个数计算2
		return count;
	}
	
}
